# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=zlib

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.8
pkgrel=2
pkgdesc="Compression library implementing the deflate compression method found in gzip and PKZIP"
arch=('any')
groups=("${MINGW_PACKAGE_PREFIX}")
license=('custom')
url="http://www.zlib.net/"
depends=("${MINGW_PACKAGE_PREFIX}-bzip2")
options=('staticlibs')
source=("http://zlib.net/current/${_realname}-${pkgver}.tar.gz"
    01-zlib-1.2.7-1-buildsys.mingw.patch
        02-no-undefined.mingw.patch
        03-dont-put-sodir-into-L.mingw.patch
        04-wrong-w8-check.mingw.patch
        05-fix-a-typo.mingw.patch)
md5sums=('44d667c142d7cda120332623eab69f40'
         '63affd37e371768c6adc6e6561e66daf'
         'cf7e49f23103a7e42c2dee113ea29674'
         'bb6c8eeb7f03978982406bca35f62ca3'
         '94f53bd0a4a4068bcedc195004e70316'
         '076b81ef097c6916cb81daa3ba8c3afa')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  grep -A 24 '^  Copyright' zlib.h > LICENSE
  
  patch -p2 -i ${srcdir}/01-zlib-1.2.7-1-buildsys.mingw.patch
  patch -p2 -i ${srcdir}/02-no-undefined.mingw.patch
  patch -p2 -i ${srcdir}/03-dont-put-sodir-into-L.mingw.patch
  patch -p2 -i ${srcdir}/04-wrong-w8-check.mingw.patch
  patch -p2 -i ${srcdir}/05-fix-a-typo.mingw.patch
}

build() {
  cd ${srcdir}/${_realname}-${pkgver}
  CHOST=${MINGW_CHOST} ./configure --prefix=${MINGW_PREFIX} --shared
  make -j1 all
  
  # build minizip
  pushd contrib/minizip > /dev/null
  autoreconf -fi
  CFLAGS+=" -DHAVE_BZIP2"
  ./configure --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
      --enable-demos \
    LIBS="-lbz2"
  make
  popd > /dev/null
}

check() {
  cd ${srcdir}/${_realname}-${pkgver}
  make test
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  make install DESTDIR="${pkgdir}"
  pushd contrib/minizip > /dev/null
  make install DESTDIR="${pkgdir}"
  popd > /dev/null
}
